package com.prs.crm.service.sale.impl;

import java.io.File;
import java.io.Serializable;
import java.util.List;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.sale.SalesContractDao;
import com.prs.crm.domain.flow.WorkFlowInstance;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.sale.QuotationSheet;
import com.prs.crm.domain.sale.SalesContract;
import com.prs.crm.domain.sale.SalesContractDetail;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.sale.SalesContractService;

public class SalesContractServiceImpl extends
		FlowServiceImpl<SalesContract, SalesContractDao> implements
		SalesContractService {

	@Override
	public void saveSalesContract(SalesContract salesContract,
			Product[] products, String[] pronames, Double[] originalCosts,
			Double[] nowCosts, Integer[] quotationNums, Double[] rebates,
			Double[] totalPrices, File file, String fileName) {
		if (file != null) {
			Attach pAttach = FileUtil.getInstance().saveFile(file, fileName,
					"", Constrains.UPLOAD_DIR_SALECONTRACT);
			salesContract.setContractAttach(pAttach.getSrc());
		}
		for (int i = 0; i < products.length; i++) {
			SalesContractDetail detail = new SalesContractDetail();
			detail.setProduct(products[i]);
			detail.setName(pronames[i]);
			detail.setOriginalCost(originalCosts[i]);
			detail.setNowCost(nowCosts[i]);
			detail.setContractNum(quotationNums[i]);
			//detail.setSendContactNum(quotationNums[i]);
			detail.setRebate(rebates[i]);
			detail.setTotalPrice(totalPrices[i]);
			//detail.setShippingState(0);
			// detail.setShippingState(0);
			detail.setReceivesNumber(0);
			detail.setReturnSate(0);
			salesContract.addDetail(detail);

		}
		salesContract.setReturnState(0);
		salesContract.setShippingState(0);
		//salesContract.setContactMonly(0.000);
		salesContract.setReceiveMoney(0.0);
		salesContract.setReceiptState(0);
		salesContract.setPayedMoney(0.0);
		super.save(salesContract);
	}

	
	@Override
	public void onProcessEnd(WorkFlowProcess process) {
		
		super.onProcessEnd(process);
	}


	public boolean valideSubmit(SalesContract entity) {

		
			return true;
		


	}

	@Override
	public void saveOrUpdate(SalesContract sales) {
		this.getDao().saveOrUpdate(sales);

	}

	@Override
	public SalesContract getSalesContract(Serializable id) {

		return this.getDao().getSalesContract(id);
	}

	@Override
	public SalesContractDetail getSalesContractDetail(Product product,
			SalesContract salesContract) {

		return this.getDao().getSalesContractDetail(product, salesContract);
	}

	@Override
	public double getSaleContract(SalesContract re) {

		return this.getDao().getSaleContract(re);
	}

	@Override
	public void saveOrUpdateDetail(SalesContractDetail salesContractDetail) {

		this.getDao().saveOrUpdateDetail(salesContractDetail);
	}

	@Override
	public List<SalesContractDetail> contractDetails(SalesContract salesContract) {

		return this.getDao().contractDetails(salesContract);
	}

	@Override
	public List<SalesContractDetail> getContractDetailsNotReceives(
			SalesContract salesContract) {
		return this.getDao().getContractDetailsNotReceives(salesContract);
	}

}
